<?php
class Advertising_redis extends Common_redis_model{
	public function __construct(array $params = array()) 
	{
		parent::__construct();
		$this->params=$params;
	}

	//保存广告数据
	function save_advertising($ad_position_sign, $data){
		$this->redis->zAdd( $this->params['channel'] . ":" . $ad_position_sign . ":list", $data['sort'], $data['id']);
		$redis_advertising_key_item = $this->params['channel'] . ":" .$ad_position_sign . ":" . $data['id'];
		$this->redis->hmset($redis_advertising_key_item, $data);
	}

	//修改广告排序
	function change_sort($ad_position_sign, $key, $val){
		$this->redis->hset($this->params['channel'] . ":" .$ad_position_sign .":". $key , 'sort', $val);//设置redis key
	}

	//保存广告位数据
	function save_ad_cate($ad_cate_sign, $data){
		$redis_live_key = $this->params['channel'] . ":list";
		$this->redis->zAdd( $redis_live_key, $data['sort'], $ad_cate_sign);
		$redis_advertising_key = $this->params['channel'] . ":" . $data['project_type'] . ":" . $ad_cate_sign;
		$this->redis->zAdd( $this->params['channel'] . ":" . $data['project_type'] . ":list", $data['sort'], $ad_cate_sign);
		$this->redis->hmset($redis_advertising_key, $data);//设置redis key
	}

	//修改广告位排序
	function change_cate_sort($ad_cate_sign, $sort){
		$redis_live_key = $this->params['channel'] . ":list";
		$this->redis->zAdd( $redis_live_key, $sort, $ad_cate_sign);
		$this->redis->zAdd( $this->params['channel'] . ":" . $data['project_type'] . ":list", $sort, $ad_cate_sign);
	}

	//删除广告位数据
	function del_ad_cate($project_type, $ad_cate_sign){
		$keys = $this->redis->keys($this->params['channel'] . ":" . $project_type . ":" . $ad_cate_sign."*");
		$this->redis->del($keys);
		$this->redis->zDelete($this->params['channel'] . ":" . $project_type . ":list", $ad_cate_sign);
	}

	//删除广告数据
	function del_ad($ad_sign, $id){
		$keys = $this->params['channel'] . ":" . $ad_sign . ":" . $id;
		$this->redis->del($keys);
		$this->redis->zDelete($this->params['channel'] . ":" . $ad_sign . ":list", $id);
	}

	//获取广告位列表
	function get_ad_cate($project_type = 0, $limit = 0, $offset = 10){
		$lists = $score = array();
		$key = $this->params['channel'] . ":" . $project_type . ":list";
		$star 	= !empty($limit)? $limit * $offset : $limit;
		$end	= !empty($offset) ? $offset * ($limit +1)  : $offset;
		$count = (int) $this->redis->zSize($key);
		if( !empty($count)) {
			$score	= $this->redis->zRevRange($key, $star, ($end-1), FALSE);
			if(!empty($score)) {
				foreach($score as $key=>$val) {
					$cate_info = $this->redis->hgetall($this->params['channel'] . ":" . $project_type . ":" . $val);
					unset($cate_info['add_time'], $cate_info['type'], $cate_info['project_type'], $cate_info['sort']);
					$lists[] = $cate_info;
				}
			}
		}
		return $lists;
	}

	function get_ad_info($ad_position_sign){
		return $this->redis->hgetall($this->params['channel'] . ":" .$ad_position_sign);
	}

	function get_ad_list($ad_position_sign, $limit = 0, $offset = 10){
		$lists = $score = array();
		$key = $this->params['channel'] . ":" . $ad_position_sign . ":list";
		$star 	= !empty($limit)? $limit * $offset : $limit;
		$end	= !empty($offset) ? $offset * ($limit +1)  : $offset;
		$count = (int) $this->redis->zSize($key);
		if( !empty($count)) {
			$score	= $this->redis->zRevRange($key, $star, ($end-1), FALSE);
			if(!empty($score)) {
				foreach($score as $key=>$val) {
					$ad_info = $this->redis->hgetall($this->params['channel'] . ":" . $ad_position_sign . ":" . $val);
					$lists[] = $ad_info;
				}
			}
		}
		return $lists;
	}

	function get_ad_key($ad_position_sign){
		$key = $this->params['channel'] . ":" . $ad_position_sign . ":list";
		$score	= $this->redis->zRevRange($key, 0, -1, FALSE);
		return $score;
	}
}